//
//  LiveSearchViewController.m
//  PrintPort
//
//  Created by hy002 on 2018/1/2.
//  Copyright © 2018年 HUAYU. All rights reserved.
//

#import "LiveSearchViewController.h"
#import "ProductModel.h"
#import "ImagelistModel.h"
#import "LiveGoodsDetailViewController.h"

static NSString *cellPath = @"LiveGoodsCell";
@interface LiveSearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UIView *navView;
@property (nonatomic, strong)UITextField *nameField;
@property (nonatomic, strong)NSArray *goodsAry;
@property (nonatomic, assign)BOOL isTH;

@end

@implementation LiveSearchViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _navView.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_navView endEditing:YES];
    _navView.hidden = YES;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    if ([_areaId integerValue] == 4)
    {
        _isTH = YES;
    }
    [self creatUI];
}

- (void)creatUI
{
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, STA_NAV_HEIGHT)];
    _navView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:_navView];
    UIButton *backBtn = [ZCControl createButtonWithFrame:CGRectMake(5, STA_NAV_HEIGHT-35, 30, 30) ImageName:@"backimg" Target:self Action:@selector(backAction) Title:nil Font:14.0];
    [_navView addSubview:backBtn];
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(50, STA_NAV_HEIGHT-35, SCREEN_WIDTH-100, 30)];
    grayView.backgroundColor = LinesColor;
    grayView.layer.cornerRadius = 15;
    grayView.layer.masksToBounds = YES;
    [_navView addSubview:grayView];
    self.nameField = [ZCControl createTextFieldWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-130, 30) placeholder:@"输入您要搜索的内容" passWord:NO leftImageView:nil rightImageView:nil Font:12.0];
    _nameField.delegate = self;
    _nameField.returnKeyType = UIReturnKeySearch;
    [grayView addSubview:_nameField];
    UIButton *searchBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-35, STA_NAV_HEIGHT-30, 20, 20) ImageName:@"ssuo" Target:self Action:@selector(searchNameAction) Title:nil Font:14.0];
    searchBtn.backgroundColor = [UIColor clearColor];
    [_navView addSubview:searchBtn];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _goodsAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_goodsAry.count > 0)
    {
        ProductModel *productModel = _goodsAry[indexPath.row];
        NSArray *imgArray = productModel.productImgList;
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 80*displaywidth, 80*displaywidth) ImageName:@""];
        if (imgArray.count > 0)
        {
            ImagelistModel *imgModel = imgArray[0];
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgModel.imgUrl]];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.masksToBounds = YES;
        [cell.contentView addSubview:imgView];
        UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 5*displaywidth, SCREEN_WIDTH-130*displaywidth, 35*displaywidth) Font:12.0 Text:productModel.productName Color:RGB(77, 77, 77)];
        titlab.lineBreakMode = NSLineBreakByTruncatingTail;
        titlab.adjustsFontSizeToFitWidth = NO;
        [cell.contentView addSubview:titlab];
//        UIButton *priceBtn = [ZCControl createButtonWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:@"jifen" Target:nil Action:nil Title:string(@" ", productModel.price) Font:14.0];
//        priceBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        priceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        priceBtn.backgroundColor = [UIColor clearColor];
//        [priceBtn setTitleColor:priceColor forState:UIControlStateNormal];
//        [cell.contentView addSubview:priceBtn];
        UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%@",productModel.priceDescribe] Color:RGB(241, 90, 36)];
        CGRect abounds = [moneyLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-200*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(12.0+fontScale)} context:nil];
        CGFloat width1 = abounds.size.width+20;
        moneyLab.frame = CGRectMake(115*displaywidth, 70*displaywidth, width1, 20*displaywidth);
        [cell.contentView addSubview:moneyLab];
        CGFloat width2 = 0;
        if (!_isTH)
        {
            moneyLab.text = [NSString stringWithFormat:@"%@ +",productModel.priceDescribe];
            UILabel *integralLab = [ZCControl createLabelWithFrame:CGRectMake(width1+120*displaywidth, 70*displaywidth, 120*displaywidth, 20*displaywidth) Font:11.0 Text:[NSString stringWithFormat:@"%@积分",productModel.integral] Color:[UIColor whiteColor]];
            integralLab.textAlignment = NSTextAlignmentCenter;
            integralLab.layer.cornerRadius = 2;
            integralLab.layer.masksToBounds = YES;
            integralLab.backgroundColor = RGB(24, 159, 186);
            [cell.contentView addSubview:integralLab];
            CGRect bounds1 = [integralLab.text boundingRectWithSize:CGSizeMake(120*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(11.0+fontScale)} context:nil];
            width2 = bounds1.size.width+10;
            if (bounds1.size.width > 120*displaywidth)
            {
                width2 = 120*displaywidth;
            }
            integralLab.frame = CGRectMake(width1+120*displaywidth, 70*displaywidth, width2, 20*displaywidth);
        }
        UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(width1+width2+140*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@" %@购买",productModel.productSales] Color:RGB(179, 179, 179)];
        [cell.contentView addSubview:numLab];
        
        UIView *lineFView = [[UIView alloc] initWithFrame:CGRectMake(0, 99*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineFView.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineFView];
        
        
    }
   

    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductModel *productModel = _goodsAry[indexPath.row];
    LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
    vc.idStr = s_str(productModel.ID);
    vc.isTeHui = _isTH;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100*displaywidth;
}


- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchNameAction
{
    [_nameField resignFirstResponder];
    if (![_nameField.text isEqualToString:@""])
    {
        [self requestGoodsSearch];
    }
    
}

- (void)requestGoodsSearch
{
    WEAKSELF;
    NSDictionary *dic = @{@"query":_nameField.text,
                          @"area":_areaId};
    [HYBaseHandler requestGoodsSearchParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _goodsAry = [ProductModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (![textField.text isEqualToString:@""])
    {
        [self requestGoodsSearch];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
